﻿
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using NHibernate.Linq;
using System;
using NHibernate;
namespace Longtop.Web.Linq {
    /// <summary>
    /// Provides a LINQ-enabled data context that works with ActiveRecord.
    /// </summary>
    public class ActiveRecordLinqContext : NHibernateContext {

        #region Public Constructors



        /// <summary>

        /// Creates and configures the context.

        /// </summary>

        public ActiveRecordLinqContext()
            : base(GetSession()) {

        }



        #endregion

        #region Private Static Methods

        /// <summary>

        /// Gets a correctly-initialized session.

        /// </summary>

        /// <returns></returns>

        private static ISession GetSession() {

            ISessionScope scope = SessionScope.Current;

            if (scope == null) {

                throw new InvalidOperationException("You must have an active SessionScope object to use this class.");
            }

            ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionFactoryHolder();

            return holder.CreateSession(typeof(ActiveRecordBase));
        }
        #endregion
    }
}